﻿using com.nm.library.Excels;
using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using NHibernateVertica;
using System.Text.RegularExpressions;
using com.nm.library.Helper;
using VerticaAPI.model;
using com.nm;

namespace VerticaAPI.Export
{
    /// <summary>
    /// 端头陈列量设置
    /// </summary>
    [Export(typeof(ExcelImport))]
    public class DWEndsDisplayMeasure: ExcelImport
    {
        public override ExcelImportType Type => ExcelImportType.ENDSDISPLAYMEASURE;

        public override Dictionary<string, ImportVerify> DictFields => dictFields;
        /// <summary>
        /// Excel字段映射
        /// </summary>
        #region Excel字段映射
        private static Dictionary<string, ImportVerify> dictFields = new List<ImportVerify> {
            new ImportVerify{ ColumnName="店铺编号",FieldName="ShopCode",DataType=typeof(string),VerifyFunc =(e,extra)=> ExcelImportHelper.GetCellMsg(e.CellValue,e.ColName,20,true,true)},
            new ImportVerify{ ColumnName="店铺名称",FieldName="ShopName",DataType=typeof(string),VerifyFunc =(e,extra)=> ExcelImportHelper.GetCellMsg(e.CellValue,e.ColName,50,false,true)},
            new ImportVerify{ ColumnName="货号",FieldName="MaterialID",DataType=typeof(string),VerifyFunc =(e,extra)=> ExcelImportHelper.GetCellMsg(e.CellValue,e.ColName,20,false,true)},
            new ImportVerify{ ColumnName="最低陈列量",FieldName="DisplayQty",DataType=typeof(string),VerifyFunc =(e,extra)=> ExcelImportHelper.GetCellMsg(e.CellValue,e.ColName,20,false,true)}
           }.ToDictionary(e => e.ColumnName, e => e);
        #endregion



        public override DataTable GetExportData(string Keys)
        {
            DataTable dt = new DataTable();
            return dt;
            //var helper = new VerticaDataHelper();
            ////var sqlSize = $@"  SELECT a.ExtrSource,a.CompanyID,a.ShopID,a.ShopCode,shop.ShopName as ShopName,a.MaterialID,a.DisplayQty FROM ndw.ndw_ShelfEndDisPlay a
            ////                      LEFT JOIN ndw.ndw_Shop shop on a.shopcode = shop.shopcode and a.ExtrSource=shop.ExtrSource and a.CompanyID=shop.CompanyID
            ////                        WHERE a.ExtrSource = '{com.nm.library.UserInfo.UserExtrSource}' AND a.CompanyID = '{com.nm.library.UserInfo.UserCompanyId}'
            ////                        {Keys}	";
            //string sql = "SELECT '' ExtrSource,'' CompanyID,'' ShopID,'' ShopCode,'' ShopName,'' MaterialID,'' DisplayQty";
            //var cmdSize = helper.GetSqlStringCommand(sql);
            //var dt = helper.ExecuteDataTable(cmdSize);
            //return dt;

        }

        public override object SaveImportData(DataTable dt, Dictionary<string, object> extraInfo, int userId)
        {
            var index = 0;
            DataTable msg = new DataTable(); 
            var result = new ImportResult
            {
                IsSuccess = true,
                ExtraInfo = "导入成功",
            };
            try
            { 
                var handler = new EndsDisplayMeasureHandler();
                var list = ModelConvertHelper<EndsDisplayMeasureModel.EndsDisplayImportModel>.ConvertToModel(dt);
                handler.ChangeList(list,out msg, out index);
                result.Message = $"成功导入{index}条数据{(dt.Rows.Count - index == 0 ? "" : $@",有{dt.Rows.Count - index}条错误数据")}";

                if (msg.Rows.Count>0)
                {
                    var path = ExcelImporMapper.GetTemplatePath(Type);
                    result.Url = ExcelUtil.ExportErrorExeclToDB(msg, path, dictFields, "端头陈列量设置", index);
                }


            }
            catch (Exception ex)
            {
                Log.Error("导入城市核心码：" + ex);
                result.IsSuccess = false;
                result.ExtraInfo = ex;
                result.Message = ex.Message;
            }
            return result;

        }
    }
}
